<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addCateShowDialogBtnFn">添加分类</el-button>
      </div>
      <!-- 分类数据表格 -->
      <el-table style="width: 100%" :data="cateList" border stripe>
        <el-table-column label="序号" type="index" width="100"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" size="mini" @click="updateCateBtn(scope.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="delCateFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- dialog对话框 -->
    <el-dialog title="添加分类" :visible.sync="dialogVisible" width="50%" @close="dialogCloseFn">
      <!-- 添加的表单 -->
      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getArtCateListAPI,
  saveArtCateAPI,
  updateArtCateAPI,
  delArtCateAPI,
} from '@/api'
export default {
  name: 'ArtCate',
  data() {
    return {
      cateList: [],
      dialogVisible: false,
      addForm: {
        // 添加表单的数据对象
        cate_name: '',
        cate_alias: '',
      },
      addRules: {
        // 添加表单的验证规则对象
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur',
          },
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur',
          },
        ],
      },
      isEdit: false,
      editId: '', //保存正在要编辑的数据的id值
    }
  },
  methods: {
    async getArtCateFn() {
      const { data: res } = await getArtCateListAPI()
      console.log(res.data)
      this.cateList = res.data
    },
    addCateShowDialogBtnFn() {
      //点击显示对话框
      this.editId = false
      this.editId = ''
      this.dialogVisible = true
    },
    //对话框确定按钮的点击事件
    confirmFn() {
      this.$refs.addRef.validate(async (valid) => {
        if (!valid) return
        if (this.isEdit) {
          //要修改
          this.addForm.id = this.editId
          const { data: res } = await updateArtCateAPI(this.addForm)
          if (res.code !== 0) return this.$message.error(res.message)
          this.$message.success(res.message)
        } else {
          //要新增
          const { data: res } = await saveArtCateAPI(this.addForm)
          if (res.code !== 0) return this.$message.error(res.message)
          this.$message.success(res.message)
        }

        this.getArtCateFn()
        this.dialogVisible = false
        //掉接口上传信息
      })
    },
    //对话框删除的点击事件
    cancelFn() {
      this.dialogVisible = false
    },
    //对话款关闭
    dialogCloseFn() {
      this.$refs.addRef.resetFields()
    }, //修改按钮的点击事件
    updateCateBtn(row) {
      console.log(row)
      this.isEdit = true
      this.editId = row.id
      this.dialogVisible = true
      this.$$nextTick(() => {
        this.addForm.cate_name = row.cate_name
        this.addForm.cate_alias = row.cate_alias
      })
    },
    //删除按钮
    async delCateFn(row) {
      const { data: res } = await delArtCateAPI(row.id)
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.getArtCateFn()
    },
  },
  created() {
    this.getArtCateFn()
  },
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
